import http from "../util/httpRequest.js"

let menuListTest = genTestMenuList()

// 获取菜单列表
export function getGoodsMenuList(params) {
	params.path = "/goods/goodsMenu/list"
	params.testData = menuListTest
	http(params)
}

// 生成测试数据
function genTestMenuList() {
	let randStr = function(len) {
		let str = ""
		const source = "abcdefghijklmnopqrstuvwxyz1234567890"
		for (let i = 0; i < len; i++) {
			str += source.charAt(Math.round(Math.random() * source.length))
		}
		return str
	}

	let menuList = []
	let categorys = []
	for (let i = 0; i < 10; i++)
		categorys.push("类别" + randStr(Math.random() * 3))
	let goodsAvatar = "https://img0.baidu.com/it/u=3074490370,3840344350&fm=26&fmt=auto"
	for (let i = 0; i < categorys.length; i++) {
		let menu = {
			name: categorys[i],
			goodsList: []
		}
		for (let j = 0; j < 1 + Math.random() * 5; j++) {
			menu.goodsList.push({
				id: Math.floor(Math.random() * 100000000),
				name: "商品" + randStr(5),
				price: 1 + Math.floor(Math.random() * 1000), // 价格：没有主属性的时候该字段生效
				count: Math.floor(Math.random() * 10), // 库存：没有主属性的时候该字段生效
				image: goodsAvatar,
				description: "description-" + Math.random(),
				propertyList: Math.random() < 0.3 ? [] : [{ // 主属性
						name: "属性-" + randStr(5),
						isMain: true,
						options: function() {
							let optionList = []
							for (let k = 0; k < 5; k++) {
								optionList.push({
									name: "选项" + randStr(3),
									price: 1 + Math.floor(Math.random() * 1000),
									count: Math.floor(Math.random() * 3),
								})
							}
							return optionList
						}()
					},
					{
						name: "属性-" + randStr(5), // 非主属性
						required: Math.random() < 0.5,
						options: function() {
							let optionList = []
							for (let k = 0; k < 5; k++) {
								optionList.push({
									name: "选项" + randStr(3),
									disabled: Math.random() < 0.3,
								})
							}
							return optionList
						}()
					},
				]
			})
		}
		menuList.push(menu)
	}
	return menuList
}
